package com.txhy.certificatechain.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.txhy.certificatechain.R;
import com.txhy.certificatechain.ui.view.user.FingerView;
import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: FingerCheckUtils.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 指纹检测工具类
 * 创建时间: 2018/9/20 15:53
 * 更新时间：2018/9/20 15:53
 * -----------------------------------------------------------------
 * 2018/9/20 15:53: 创建 FingerCheckUtils.java (侯建军);
 * -----------------------------------------------------------------
 */
public class FingerCheckUtils {
    private FingerView fingerView;
    private Context context;
    private Dialog fingDialog;
    private ImageView img_img;
    private TextView tv_text;
    private long oldTime;
    public FingerCheckUtils(FingerView fingerView, Context context) {
        this.fingerView = fingerView;
        this.context = context;
    }
    /**
     * 显示指纹  int  0 指纹验证失败   1验证ok  2没有指纹功能  3有指纹功能没录入
     */
    FingerprintIdentify mFingerprintIdentify;
    public Dialog showFingerDialog() {
        fingDialog = new Dialog(context, R.style.MyDialog);
        LinearLayout root = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.dialog_finger, null);
        img_img = root.findViewById(R.id.img_img);
        tv_text = root.findViewById(R.id.tv_text);
        root.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideFingerDialog();
            }
        });
        fingDialog.setContentView(root);
        Window dialogWindow = fingDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        fingDialog.setCancelable(false);
        dialogWindow.setWindowAnimations(R.style.dialogstyle); // 添加动画
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度

        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        fingDialog.show();
        mFingerprintIdentify = new FingerprintIdentify(context); // 构造对象
//判断是否有指纹   没有则注册ok
        if (!mFingerprintIdentify.isHardwareEnable()) {
            fingerView.getFingerResult(1);
            ToastUtil.showLong(context, UIUtils.getString(R.string.nindeshouji));
            return fingDialog;
        }
        //判断是否录入指纹
        if (!mFingerprintIdentify.isRegisteredFingerprint()) {
            fingerView.getFingerResult(3);
            ToastUtil.showLong(context, UIUtils.getString(R.string.nindeshoujihai));
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
        }
        mFingerprintIdentify.startIdentify(5, listener);
        return fingDialog;
    }
    /**
     * 隐藏指纹
     */
    public void hideFingerDialog() {
        if (fingDialog != null) {
//            mFingerprintIdentify.resumeIdentify(); // 恢复指纹识别并保证错误次数不变
            fingDialog.dismiss();
        }
    }
    private BaseFingerprint.FingerprintIdentifyListener listener = new BaseFingerprint.FingerprintIdentifyListener() {
        @Override
        public void onSucceed() {
            tv_text.setText("比对成功");
            img_img.setImageResource(R.mipmap.success);
            fingerView.getFingerResult(1);
        }
        @Override
        public void onNotMatch(int availableTimes) {
            Toast.makeText(context, "您还有" + availableTimes + "次比对次数", Toast.LENGTH_LONG).show();

        }
        @Override
        public void onFailed(boolean isDeviceLocked) {
            if (!isDeviceLocked) {
                fingDialog.dismiss();
                oldTime = System.currentTimeMillis();
            }
        }
        @Override
        public void onStartFailedByDeviceLocked() {
            fingDialog.dismiss();
            int time = (int) (30000 - (System.currentTimeMillis() - oldTime)) / 1000;
            if (time <= 0) {
                time = 1;
            }
            Toast.makeText(context, "请确保指纹正确、清洁" + time + "秒后重试", Toast.LENGTH_LONG).show();
        }
    };
}
